import 'package:dio/dio.dart';

import '../../constants/app_constants.dart';

///最基础的联网请求功能
class ApiClient {
  final dio = Dio();

  ///get请求
  ///返回值是 Future<Response>
  Future<Response> get(String path, {Map<String, dynamic>? body}) async {
    var mPath = '${AppConstants.baseUrl}$path';
    if (body == null) {
      return await dio.get(mPath);
    } else {
      return await dio.get(mPath, queryParameters: body);
    }
  }

  ///post请求
  ///返回值是 Future<Response>
  Future<Response> post(String path, Map<String, dynamic>? body) async {
    var mPath = '${AppConstants.baseUrl}$path';
    if (body == null) {
      return await dio.post(mPath);
    } else {
      return await dio.post(mPath, queryParameters: body);
    }
  }

  Future<Response> download(String url, String savePath, ProgressCallback? onReceiveProgress) async {
    return await dio.download(
      url,
      savePath,
      onReceiveProgress: onReceiveProgress,
      options: Options(
        responseType: ResponseType.bytes,
        followRedirects: true,
        validateStatus: (status) {
          return status! < 500;
        },
      ),
    );
  }
}
